from lxml.html import fromstring, tostring

from datetime import datetime
from django.utils.dateformat import format

from urllib2 import urlopen

from shackapi.settings import SHACK_URLS

def stripchars(string):
    #TODO: this should be a regex
    badchars = ('\r', '\n', '\t', '&#13;')
    for char in badchars:
        string = string.replace(char, '')
    return string

def allstories():
    page = fromstring(urlopen(SHACK_URLS['base']).read())
    
    return parse_stories(page.cssselect('div.story'))
    
def one_story(story_id):
    #'full_story' : 'onearticle.x/%(story_id)i',
    url = '%s%s' % (SHACK_URLS['base'],
                    SHACK_URLS['full_story'] % {'story_id': int(story_id)})
    page = fromstring(urlopen(url).read())
    
    return parse_stories(page.cssselect('div.story'))


def parse_stories(nodes):
    stories = []

    
    for node in nodes:
        story = {}

        toremove = []
        toremove.extend(node.cssselect('a.readmore'))
        toremove.extend(node.cssselect('script'))
        toremove.extend(node.cssselect('p.videoembed'))
        
        for item in toremove:
            item.getparent().remove(item)
        
        link = node.cssselect('h1 a[href*=onearticle]')[0]

        story['id'] = link.get('href').split('/')[-1]
        #Shacknews actually won't 404 on a bad story ID, but it does use a story
        #ID of 0
        if story['id'] == '0':
            continue
        
        story['name'] = link.text_content()

        bodynode = node.cssselect('div.body')[0]
        
        #XXX Bad hack to get rid of <div class="body"> and </div> wrapper.
        #TODO use .drop_tag() from lxml to get rid of the outer tag
        story['body'] = stripchars(tostring(bodynode))[18:-6]
        
        story['preview'] = stripchars(bodynode.text_content())
        
        #Jun 09, 2010 8:00pm CST
        #XXX we should make use of pytz here and properly handle time zone data
        story_date = node.cssselect('span.date')[0].text_content().replace(' CST', '')
        #c format is ISO 8601
        story['date'] = format(datetime.strptime(story_date, '%b %d, %Y %I:%M%p'), 'c')
        
        cctext = node.cssselect('span.commentcount')[0].text_content().replace(' Comments', '').replace(' Comment','')
        story['comment_count'] = 0 if cctext == 'No' else int(cctext)
        
        stories.append(story)
    
    return stories